package com.biqee.service.tb.impl;

import java.util.Date;

import com.biqee.model.TaobaoParseReulst;
import com.biqee.model.TaobaoShopParseResult;
import com.biqee.model.db.TbItem;
import com.biqee.model.db.TbShop;
import com.biqee.service.crawler.PageFetcher;
import com.biqee.service.tb.TaobaoParseService;
import com.biqee.service.tb.TaobaoShopParseService;
import com.biqee.service.tb.TbItemParseService;
import com.biqee.service.tb.TbItemService;
import com.biqee.service.tb.TbShopService;

public class TaobaoParseServiceImpl implements TaobaoParseService {

	@Override
	public TaobaoParseReulst parse(String itemId) {
		if(itemId == null) {
			return null;
		}
		
		TbItem tbItem = parseTbItem(itemId);
		TbShop tbShop = parseTbShop(tbItem);
		
		TaobaoParseReulst taobaoParseReulst = new TaobaoParseReulst();
		taobaoParseReulst.setTbItem(tbItem);
		taobaoParseReulst.setTbShop(tbShop);

		return taobaoParseReulst;
	}
	
	private static final String ITEM_URL_PREFIX = "http://item.taobao.com/item.htm?id=";
	private TbItem parseTbItem(String itemId) {
		boolean isNeedParse = true;
		TbItem tbItemInDb = tbItemService.getByTbItemId(itemId);
		if(tbItemInDb != null) {
		
			Date gmtModified = tbItemInDb.getGmtModified();
			isNeedParse = isNeedParse(gmtModified);
		}
		
		if(isNeedParse) {
			String itemUrl = ITEM_URL_PREFIX + itemId;
			String itemHtml = pageFetcher.getAsStringWithSpiderHeader(itemUrl, null);
			
			TbItem tbItem = tbItemParseService.parse(itemHtml);
			// 如果解析失败，使用原有解析结果
			if(tbItem != null) {
				if(tbItemInDb!=null){
					tbItem.setId(tbItemInDb.getId());
				}
				tbItemService.save(tbItem);
				return tbItem;
			}
		}
		
		return tbItemInDb;
	}
	
	private static final String SHOP_URL_PREFIX = "http://store.taobao.com/?shop_id=";
	private TbShop parseTbShop(TbItem tbItem) {
		if(tbItem == null) {
			return null;
		}
		
		String shopUrl = null;
		TbShop tbShopInDb = null;
		String tbShopId = tbItem.getTbShopId();
		if(!TbItem.UNKNOWN_SHOP_ID.equalsIgnoreCase(tbShopId)) {
			shopUrl = SHOP_URL_PREFIX + tbShopId;
			tbShopInDb = tbShopService.readByTbShopId(tbShopId);
		}
		
		boolean isNeedParse = true;
		if(tbShopInDb != null) {
			Date gmtModified = tbShopInDb.getGmtModified();
			isNeedParse = isNeedParse(gmtModified);
		}
		
		if(shopUrl == null) {
			shopUrl = tbItem.getTbShopUrl();
		}
		if(isNeedParse) { 
			String shopHtml = pageFetcher.getAsStringWithSpiderHeader(shopUrl, null);
			
			TaobaoShopParseResult tbShopParseResult = taobaoShopParseService.parse(shopHtml);
			// 如果解析失败，使用原有解析结果
			if(tbShopParseResult != null) {
				TbShop tbShop = new TbShop();
				tbShop.setFinalGrade(tbShopParseResult.getFinalGrade());
				tbShop.setMatchGrade(tbShopParseResult.getMatchGrade());
				tbShop.setName(tbShopParseResult.getName());
				tbShop.setNick(tbShopParseResult.getName());
				tbShop.setSellerGrade(tbShopParseResult.getSellerGrade());
				tbShop.setSellerGradePic(tbShopParseResult.getSellerGradePic());
				tbShop.setServiceGrade(tbShopParseResult.getServiceGrade());
				tbShop.setTbShopId(tbShopParseResult.getShopId());
				tbShop.setSpeedGrade(tbShopParseResult.getSpeedGrade());
				tbShop.setType(tbShopParseResult.getType());
				tbShop.setUrl(tbShopParseResult.getUrl());
				
				tbShopService.save(tbShop);
				return tbShop;
			}
		}
		
		return tbShopInDb;
	}
	
	// 10 小时
	private static final long PARSE_DIFF = 10 * 60 * 60 * 1000;
	private boolean isNeedParse(Date gmtModified) {
		long lastModified = gmtModified.getTime();
		long now = new Date().getTime();
		return (now - lastModified > PARSE_DIFF);
	}
	
	private TbItemService tbItemService;
	
	public void setTbItemService(TbItemService tbItemService) {
		this.tbItemService = tbItemService;
	}

	private TbShopService tbShopService;

	public void setTbShopService(TbShopService tbShopService) {
		this.tbShopService = tbShopService;
	}

	private TbItemParseService tbItemParseService;

	public void setTbItemParseService(TbItemParseService tbItemParseService) {
		this.tbItemParseService = tbItemParseService;
	}

	private TaobaoShopParseService taobaoShopParseService;

	public void setTaobaoShopParseService(
			TaobaoShopParseService taobaoShopParseService) {
		this.taobaoShopParseService = taobaoShopParseService;
	}
	
	private PageFetcher pageFetcher;

	public void setPageFetcher(PageFetcher pageFetcher) {
		this.pageFetcher = pageFetcher;
	}
	
	
}
